﻿/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.ComponentModel;
using System.Windows.Forms;
using BrightIdeasSoftware;
using TeachMe.Application.Properties;

namespace TeachMe.Application.Controls
{
    public class DirectionComboBox : ComboBox, IControlValueProvider
    {
        private class DirectionItem
        {
            public EntryTextDirection EntryTextDirection { get; set; }
            public string Caption { private get; set; }

            public override string ToString()
            {
                return Caption;
            }
        }

        private bool _oneWay;

        public DirectionComboBox()
        {
            DropDownStyle = ComboBoxStyle.DropDownList;
        }

        public void FillDirections(string from, string to)
        {
            Items.Clear();

            _oneWay = String.Compare(from, to, true) == 0;

            if (!_oneWay)
            {
                Items.Add(new DirectionItem { EntryTextDirection = EntryTextDirection.FromTo, Caption = String.Format(Resources.OneDirection, from, to) });
                Items.Add(new DirectionItem { EntryTextDirection = EntryTextDirection.ToFrom, Caption = String.Format(Resources.OneDirection, to, from) });
                Items.Add(new DirectionItem { EntryTextDirection = EntryTextDirection.Both, Caption = String.Format(Resources.BothDirections, from, to) });
            }
            else
            {
                Items.Add(new DirectionItem { EntryTextDirection = EntryTextDirection.FromTo, Caption = String.Format(Resources.OneDirection, from, to) });
            }

            Enabled = !_oneWay;
        }

        [Browsable(false)]
        public EntryTextDirection EntryTextDirection
        {
            get
            {
                DirectionItem selection = SelectedItem as DirectionItem;
                if (selection == null)
                {
                    throw new InvalidOperationException();
                }

                return selection.EntryTextDirection;
            }
            set
            {
                foreach (DirectionItem priorityItem in Items)
                {
                    if (priorityItem.EntryTextDirection == value)
                    {
                        SelectedItem = priorityItem;
                    }
                }
            }
        }

        public bool OneWay
        {
            get { return _oneWay; }
        }

        public object GetValue()
        {
            return Text;
        }
    }
}
